﻿Imports System.Net
Imports DotRas

Module Dialer_VPN

    Public Function VPN_CALL(ByVal EntryName As String, ByVal VPN_SERVICE_IP As String, ByVal VPN_CONT_MODE As Boolean, ByVal VPN_CONT_ROUTE As Boolean, ByVal VPN_USERNAME As String, ByVal VPN_PASSWORD As String) As String
        ' 打开电话簿来使用
        VPNClient.AllUsersPhoneBook.Open()
        '创建拨号
        Dim entry As RasEntry = RasEntry.CreateVpnEntry(EntryName, "vpnvip.imzhou.com", RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn))
        '添加创建的拨号到电话簿
        VPNClient.AllUsersPhoneBook.Entries.Add(entry)
        '-------------------------------------------------------------------
        VPNClient.ToolStripStatusLabel1.Text = ""
        ' 拨号
        VPNClient.Dialer.EntryName = EntryName
        VPNClient.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers)

        Try
            ' 设置拨号器应该使用的凭据：帐号密码。
            VPNClient.Dialer.Credentials = New NetworkCredential("coolie1978", "coolie1978")
            VPNClient.Dialer.DialAsync()
            VPNClient.btn_Connection_Cancel.Enabled = True
        Catch ex As Exception
            VPNClient.ToolStripStatusLabel1.Text = ex.ToString()
        End Try
    End Function

    '   Private Sub Dialer_StateChanged(ByVal sender As System.Object, ByVal e As DotRas.StateChangedEventArgs) Handles Dialer.StateChanged
    '       VPNClient.ToolStripStatusLabel1.Text = e.State.ToString() + Chr(13) + Chr(10)
    '   End Sub
    '
    '   Private Sub Dialer_DialCompleted(ByVal sender As System.Object, ByVal e As DotRas.DialCompletedEventArgs) Handles Dialer.DialCompleted
    '       If (e.Cancelled) Then
    '           VPNClient.ToolStripStatusLabel1.Text = "连接已取消！"
    '       ElseIf (e.TimedOut) Then
    '           VPNClient.ToolStripStatusLabel1.Text = "连接失败！"
    '       ElseIf (e.Error IsNot Nothing) Then
    '           VPNClient.ToolStripStatusLabel1.Text = e.Error.ToString()
    '       ElseIf (e.Connected) Then
    '           VPNClient.ToolStripStatusLabel1.Text = "连接成功！"
    '       End If
    '
    '       If (Not e.Connected) Then
    '           VPNClient.btn_Connection_Cancel.Enabled = False
    '       End If
    '   End Sub
End Module
